﻿        var countryid;

$(function () {


    init_autocomplete_area_search();

    init_toggle_box();

    init_search_list();


});

function init_search_list () {

//    $("#main_search .div_downlist").live("mouseenter", function () {
//        $("#DlsAppType").attr('size','3');
//    });

//    $("#main_search .div_downlist").live("mouseleave", function () {
//        $("#DlsAppType").attr('size','1');
//    });
}




function init_autocomplete_area_search(f) {

    var last_search = '';
    $('#HidMainSearchSel').val('');

    $.ui.autocomplete.prototype._renderItem = function( ul, item) {
        var re = new RegExp("\\b" + this.term ,"ig");
        var t = item.label.replace(re,"<span style='font-weight:bold;color:Red;'>$&</span>");
        return $( "<li></li>" )
            .data( "item.autocomplete", item )
            .append( "<a>" + t + "</a>" )
            .appendTo( ul );
    };

    $.widget("custom.catcomplete", $.ui.autocomplete, {
        _renderMenu: function (ul, items) {
            var self = this,
				        currentCategory = "";
            $.each(items, function (index, item) {
                if (item.category != currentCategory) {
                    ul.append("<li class='ui-autocomplete-category'>" + item.category + "</li>");
                    currentCategory = item.category;
                }
                self._renderItem(ul, item);
            });
        }
    });



    $("#TxtMainSearch").catcomplete({
        source: function (request, response) {
            $.ajax({
                url: "/webservice/ajax.asmx/AutoCompleteSearchArea",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                data: '{"k":"' + request.term + '","c":"' + countryid + '"}',
                success: function (data) {
                    response (data.d);
                    //alert('fine'); 
                },
            });
        },
        minLength: 2,
        delay : 100,
        search: function(event, ui) {
            //alert(countryid);
        },
        select: function (event, ui) {
            //alert(ui.item.id);
            $('#HidMainSearchSel').val(ui.item.id);
            $('#BtnMainSearch').click();
            //__doPostBack(SearchButtonName,'');

        }
    });

/*   questa parte serve se devo verificare se dopo la selezione cambiano i dati e quindi toglie l'id 
    .keyup(function (k) {
        if (last_search != $("#main_search").val() ) {
            $('#main_search_sel').val('');
        }
        last_search= $("#main_search").val();
    });
*/


}

